﻿using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class DeathModelRepository : iDeathModelRepository
    {
        private readonly AgricultureDbContext _context;
       
        public DeathModelRepository(AgricultureDbContext _context)
        {
            this._context = _context;
        }

        
      

        

        /// <summary>
        /// 根据耳号获取档案信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<Sheepfilemodel>> GetBuildByEar()
        {
            try
            {
                var list = await _context.Sheepfilemodels.ToListAsync();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取档案信息的耳号
        /// </summary>
        /// <returns></returns>
        public async Task<List<Sheepfilemodel>> GetEagShow()
        {
            try
            {
                var list = await _context.Sheepfilemodels.ToListAsync();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示，查询，分页功能
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Deathmodel>> GetShow()
        {
            try
            {
                return _context.Deathmodels.Where(x => x.Isdel == false);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回受影响行数</returns>
        public Task<int> Delete(int id)
        {
            try
            {
                var list = _context.Deathmodels.FirstOrDefault(x => x.DeathId == id);
                if (list != null)
                {
                    list.Isdel = true;
                    _context.Update(list);
                }
                return _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
       
        /// <summary>
        /// 反填功能
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>通过ID查找到的列表</returns>
        public async Task<Deathmodel> Find(int id)
        {
            try
            {
                var list = _context.Deathmodels.FirstOrDefault(x => x.DeathId == id);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

       
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="Death">修改实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateAsync(Deathmodel Death)
        {
            try
            {
                _context.Entry(Death).State = EntityState.Modified;

                return await _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 死亡新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回受影响行数</returns>
        
        public async Task<int> Add(List<Deathmodel> entity)
        {
            try
            {
                await _context.AddRangeAsync(entity);
                return await _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Task<int> DeleteRange(IEnumerable<int> ids)
        {
            try
            {
                foreach (var item in ids)
                {
                    var list = _context.Deathmodels.FirstOrDefault(x => x.DeathId == item);
                    if (list != null)
                    {
                        list.Isdel = true;
                        _context.Update(list);
                    }
                }
                return _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
